package tts.controller.roles;

import org.springframework.beans.factory.annotation.Autowired;

import tts.controller.RedirectAction;
import tts.dao.RolesHome;
import tts.model.Roles;
import tts.model.Users;


public class Delete extends RedirectAction{

	private static final long serialVersionUID = 7628675327304635055L;
	@Autowired
	private RolesHome rolesHome;
	private int roles_id;

	//fields
	public int getRoles_id() {
		return roles_id;
	}

	public void setRoles_id(int roles_id) {
		this.roles_id = roles_id;
	}

	public String delete() {
		try {
			Roles role = rolesHome.findById(this.roles_id);
			for (Users u : role.getUsersList())
				u.getRolesList().remove(role);
			rolesHome.remove(role);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("role.deleted"));
		return SUCCESS;
	}
}